package com.dayu.recovery.service.impl;

import java.util.*;

import com.dayu.basic.domain.BasicTestValues;
import com.dayu.basic.mapper.BasicTestValuesMapper;
import com.dayu.common.core.domain.entity.SysUser;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.RecoveryGoodsRecord;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import com.dayu.recovery.mapper.RecoveryGoodsRecordMapper;
import com.dayu.system.mapper.SysUserMapper;
import com.dayu.utils.BarCodeUtils;
import com.dayu.utils.RandomOrderUtils;
import com.dayu.utils.constant.StepLogConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.RecoveryScatteredMapper;
import com.dayu.recovery.domain.RecoveryScattered;
import com.dayu.recovery.service.IRecoveryScatteredService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 散户验机Service业务层处理
 *
 * @author HK
 * @date 2021-04-16
 */
@Service
public class RecoveryScatteredServiceImpl implements IRecoveryScatteredService
{
    @Autowired
    private RecoveryScatteredMapper recoveryScatteredMapper;
    @Autowired
    private RecoveryGoodsMapper recoveryGoodsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Resource
    private BasicTestValuesMapper valuesMapper;

    @Resource
    private RecoveryGoodsRecordMapper goodsRecordMapper;
    /**
     * 查询散户验机
     *
     * @param scatteredId 散户验机ID
     * @return 散户验机
     */
    @Override
    public RecoveryScattered selectRecoveryScatteredById(Long scatteredId)
    {
        return recoveryScatteredMapper.selectRecoveryScatteredById(scatteredId);
    }

    /**
     * 查询散户验机列表
     *
     * @param recoveryScattered 散户验机
     * @return 散户验机
     */
    @Override
    public List<RecoveryScattered> selectRecoveryScatteredList(RecoveryScattered recoveryScattered)
    {
        return recoveryScatteredMapper.selectRecoveryScatteredList(recoveryScattered);
    }

    @Override
    public List<RecoveryScattered> exportScatteredList(RecoveryScattered recoveryScattered) {
        List<RecoveryScattered> list = selectRecoveryScatteredList(recoveryScattered);
        List<SysUser> userList = sysUserMapper.selectUserList(new SysUser());
        list.forEach(it -> {
            userList.forEach(item -> {
                if(it.getCreateId().equals(item.getUserId())) {
                    it.setCreateName(item.getNickName());
                }
            });
        });
        return list;
    }

    /**
     * 新增散户验机
     *
     * @param recoveryScattered 散户验机
     * @return 结果
     */
    @Override
    public int insertRecoveryScattered(RecoveryScattered recoveryScattered)
    {
        recoveryScattered.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        recoveryScattered.setCreateTime(DateUtils.getNowDate());
        return recoveryScatteredMapper.insertRecoveryScattered(recoveryScattered);
    }

    /**
     * 修改散户验机
     *
     * @param recoveryScattered 散户验机
     * @return 结果
     */
    @Override
    public int updateRecoveryScattered(RecoveryScattered recoveryScattered)
    {
        return recoveryScatteredMapper.updateRecoveryScattered(recoveryScattered);
    }

    /**
     * 批量删除散户验机
     *
     * @param scatteredIds 需要删除的散户验机ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryScatteredByIds(Long[] scatteredIds)
    {
        return recoveryScatteredMapper.deleteRecoveryScatteredByIds(scatteredIds);
    }

    /**
     * 删除散户验机信息
     *
     * @param scatteredId 散户验机ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryScatteredById(Long scatteredId)
    {
        return recoveryScatteredMapper.deleteRecoveryScatteredById(scatteredId);
    }

    /**
     * 回收商品
     * @param scattered 回收的数据
     * @return 结果
     */
    @Override
    @Transactional
    public int collectScatteredGoods(RecoveryScattered scattered) {
        RecoveryScattered selScattered = selectRecoveryScatteredById(scattered.getScatteredId());
        RecoveryGoods addGoods = new RecoveryGoods();
        Set<String> ids = RandomOrderUtils.randomOrderNumSet(1);
        if(!ObjectUtils.isEmpty(scattered.getTestValues())) {
            String[] arr = scattered.getTestValues().split(",");
            BasicTestValues query = new BasicTestValues();
            Map<String, Object> map = new HashMap<>();
            map.put("ids", arr);
            query.setParams(map);
            List<BasicTestValues> list = valuesMapper.selectBasicTestValuesList(query);
            Set<String> str = new HashSet<>();
            list.forEach(item -> str.add(item.getValuesName()));
            String testInfo = str.toString();
            addGoods.setTestInfo(testInfo.substring(1, testInfo.length() - 1));
        }

        addGoods.setGoodsId(ids.stream().iterator().next());
        addGoods.setBarCode(BarCodeUtils.getBarCodeUrl(addGoods.getGoodsId()));
        addGoods.setCreateTime(new Date());
        addGoods.builderByScattered(selScattered);  //数据复制
        addGoods.setSupplierName(scattered.getSupplierName());
        selScattered.setStatus(1);         //确认生成
        recoveryGoodsMapper.insertRecoveryGoods(addGoods);

        //验机日志
        RecoveryGoodsRecord record = new RecoveryGoodsRecord();
        record.setGoodsId(addGoods.getGoodsId());
        record.setGoodsSn(addGoods.getGoodsSn());
        record.setTestInfo(addGoods.getTestInfo());
        record.setGoodsName(addGoods.getGoodsName());
        record.setSpecs(addGoods.getPrinting());
        record.setCostPrice(addGoods.getCostPrice());
        record.setType(1);
        SysUser user = sysUserMapper.selectUserById(selScattered.getCreateId());
        if(!ObjectUtils.isEmpty(user)) {
            record.setCreateName(user.getNickName());
            record.setCreateBy(user.getUserName());
        }
        record.setCreateTime(selScattered.getCreateTime());
        record.setUpdateTime(new Date());
        goodsRecordMapper.insertRecoveryGoodsRecord(record);


        return updateRecoveryScattered(selScattered);
    }
}
